package com.wxs.shiro.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.wxs.common.pojo.Resource;
import com.wxs.common.pojo.Role;
import com.wxs.common.service.ResourceService;
import com.wxs.common.service.RoleService;
import com.wxs.common.utils.DataGridModel;
import com.wxs.common.utils.DataResult;
import com.wxs.common.utils.JsonResult;

/**
 * 角色管理
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/role/")
public class RoleController {
	@Autowired
	private RoleService roleService;

	@Autowired
	private ResourceService resourceService;

	@RequestMapping("main.jhtml")
	public ModelAndView main() {
		return new ModelAndView("role/list");
	}

	@ResponseBody
	@RequestMapping("list.jhtml")
	public DataResult list(String name, Boolean enabled, DataGridModel dgm) {
		return roleService.queryPage(name, enabled, dgm);
	}

	@ResponseBody
	@RequestMapping("listEnabled.jhtml")
	public List<Role> listEnabled(String name, Boolean enabled, DataGridModel dgm) {
		return roleService.findAllByEnabled(true);
	}

	@RequestMapping("edit.jhtml")
	public ModelAndView edit(@RequestParam(value = "id", required = true) Integer id) {
		ModelAndView mv = new ModelAndView("role/edit");
		mv.addObject("role", roleService.findById(id));
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "saveEdit.jhtml", produces = "text/html;charset=UTF-8")
	public String saveEdit(Role role) {
		try {
			if (roleService.editRole(role) > 0) {
				return JsonResult.toSuccess(null, "修改成功！");
			} else {
				return JsonResult.toError(null, "修改失败！");
			}
		} catch (Exception e) {
			return JsonResult.toError(e.getMessage(), "修改失败！");
		}
	}

	@ResponseBody
	@RequestMapping("doCheckName")
	public Boolean checkName(@RequestParam(value = "name", required = true) String name, Integer id) {
		return roleService.checkName(name, id);
	}

	@ResponseBody
	@RequestMapping(value = "doDeleteRole", produces = "text/html;charset=UTF-8")
	public String doDeleteRole(@RequestParam(value = "id", required = true) Integer id) {
		try {
			roleService.deleteById(id);
			return JsonResult.toSuccess(null, "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.toError(e.getMessage(), "删除失败！");
		}
	}

	@RequestMapping("add.jhtml")
	public String add() {
		return "role/add";
	}

	@ResponseBody
	@RequestMapping(value = "saveAdd.jhtml", produces = "text/html;charset=UTF-8")
	public String saveAdd(Role role) {
		try {
			role.setId(null);
			if (roleService.saveRole(role) > 0) {
				return JsonResult.toSuccess(null, "保存成功！");
			} else {
				return JsonResult.toError(null, "保存失败！");
			}
		} catch (Exception e) {
			return JsonResult.toError(e.getMessage(), "保存失败！");
		}
	}

	@RequestMapping("set.jhtml")
	public ModelAndView set(@RequestParam(value = "id", required = true) Integer id) {
		ModelAndView mv = new ModelAndView("role/set");
		mv.addObject("roleid", id);
		List<Resource> ls = resourceService.listAllByRoleid(id);
		List<Integer> ids = new ArrayList<>();
		getIds(ls, ids);
		mv.addObject("ids", JSON.toJSONString(ids));
		return mv;
	}

	private void getIds(List<Resource> ls, List<Integer> ids) {
		for (Resource resource : ls) {
			ids.add(resource.getId());
		}
	}

	@ResponseBody
	@RequestMapping(value = "saveResource.jhtml", produces = "text/html;charset=UTF-8")
	public String saveRoleResource(String[] resourceids, Integer roleid) {
		try {
			roleService.saveRoleResource(resourceids, roleid);
			return JsonResult.toSuccess(null, "保存成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.toError(null, "保存失败！");
		}
	}
}
